package com.example.leetdemo.finash;

public class Solution14 {

    public String longestCommonPrefix(String[] strs) {
        int length = 1000007;
        for (int i = 0; i < strs.length; i++) {
            length = Math.min(strs[i].length(), length);
        }
        for (int i = 0; i < length; i++) {
            String a = strs[0].substring(0, i + 1);
            for (int j = 1; j < strs.length; j++) {
                if (!strs[j].substring(0, i + 1).equals(a)) {
                    if (i == 0) {
                        return "";
                    }
                    return a.substring(0, a.length() - 1);
                }
            }
        }
        return strs[0].substring(0, length);
    }

    public static void main(String[] args) {
        Solution14 solution14 = new Solution14();
        System.err.println(solution14.longestCommonPrefix(new String[] {"aab", "aac", "aad"}));
        System.err.println(solution14.longestCommonPrefix(new String[] {"aab", "aac", "a"}));
        System.err.println(solution14.longestCommonPrefix(new String[] {"aa", "aa", "aa"}));

    }
}
